using System.Collections.Generic;
using CinemaDirector;
using UnityEngine;

public class MultiplayerGameDone : MonoBehaviour
{
	public delegate void OnDone();

	public class GameParams
	{
		public int flag1;

		public int flag2;

		public string name1;

		public string name2;

		public int winnings;

		public int winnerIndex = -1;

		public string level1;

		public string level2;

		public string elo1;

		public string elo2;

		public bool hideStars;

		public string opponentImageUrl;

		public string prevLevelName;

		public string newLevelName;

		public float prevLevel;

		public float currentLevel;

		public float prevMissionProgress;

		public float curMissionProgress;

		public string missionName = string.Empty;

		public GGStoryAchivementBase mission;

		public OnDone onDone;

		public bool isSkillIncrease
		{
			get
			{
				return currentLevel > prevLevel;
			}
		}

		public bool isNextLevelReached
		{
			get
			{
				return Mathf.FloorToInt(currentLevel) > Mathf.FloorToInt(prevLevel);
			}
		}

		public bool giveMoney
		{
			get
			{
				return winnings > 0;
			}
		}

		public bool isMissionProgressed
		{
			get
			{
				return curMissionProgress > prevMissionProgress;
			}
		}

		public bool isMissionPassed
		{
			get
			{
				return curMissionProgress >= 1f;
			}
		}

		public static GameParams FromMatchParameters()
		{
			GameParams gameParams = new GameParams();
			gameParams.flag1 = MatchParameters.InitParameters.player1Flag;
			gameParams.flag2 = MatchParameters.InitParameters.player2Flag;
			gameParams.name1 = MatchParameters.InitParameters.player1Name.ToUpper();
			gameParams.name2 = MatchParameters.InitParameters.player2Name.ToUpper();
			gameParams.elo1 = GGPlayerSettings.instance.GetDisplayEloRating().ToString();
			gameParams.elo2 = GGPlayerSettings.instance.GetDisplayEloRating(MatchParameters.InitParameters.multiplayerParams.eloRank).ToString();
			gameParams.opponentImageUrl = ((MatchParameters.InitParameters.matchType != MatchType.MultiPlayer || !GGPlayerSettings.instance.useProfilePictureInMultiplayer) ? string.Empty : MatchParameters.InitParameters.multiplayerParams.opponentImageUrl);
			return gameParams;
		}

		public GameParams SetWinnerIndex(int winnerIndex)
		{
			this.winnerIndex = winnerIndex;
			return this;
		}

		public GameParams SetMissionProgress(string missionName, float prev, float current)
		{
			prevMissionProgress = prev;
			curMissionProgress = current;
			this.missionName = missionName;
			return this;
		}

		public GameParams SetPlayerLevels(string l1, string l2)
		{
			hideStars = false;
			level1 = l1;
			level2 = l2;
			return this;
		}

		public GameParams SetLevelNames(string l1, string l2)
		{
			prevLevelName = l1;
			newLevelName = l2;
			return this;
		}

		public GameParams HidePlayerStars()
		{
			hideStars = true;
			return this;
		}

		public GameParams SetLevels(float prevLevel, float curLevel)
		{
			this.prevLevel = prevLevel;
			currentLevel = curLevel;
			return this;
		}

		public GameParams SetDelegate(OnDone onDone)
		{
			this.onDone = onDone;
			return this;
		}

		public GameParams SetWinnings(int winnings)
		{
			this.winnings = winnings;
			return this;
		}
	}

	public Cutscene pronounceWinner;

	public Cutscene addSkill;

	public Cutscene newSkillLevel;

	public Cutscene exitSkillLevel;

	public Cutscene addCoins;

	public Cutscene missionAnim;

	public Cutscene missionPassedAnim;

	public Cutscene missionExitAnim;

	public Cutscene gameComplete;

	public GGTweenPositionObjects toWinnerMoney;

	public GGActionMatchPositionEvent toWinnerLabel;

	public GGTweenFill tweenFill;

	public GGActionSetTextEvent nextLevelTextTitle;

	public GGActionSetTextEvent nextLevelTextLevel;

	public GGTweenFill tweenMissionFill;

	public GGActionSetTextEvent setLevelText;

	public UILabel winLabel;

	public GameObject pots;

	public UILabel drawLabel;

	public UISprite flag1;

	public UISprite flag2;

	public UITexture profileImage1;

	public UITexture profileImage2;

	public UILabel name1;

	public UILabel name2;

	public UILabel levelTitle;

	public UILabel levelSubtitle;

	public UISprite levelSlider;

	public GameObject touchToContinue;

	public UILabel player1Level;

	public UILabel player2Level;

	public GameObject star1;

	public GameObject star2;

	public UILabel missionTitle;

	public UISprite missionFill;

	public GameObject messageRematchButton;

	public UISprite rematchButton;

	public UILabel rematchButtonLabel;

	public UILabel playerStarMatch;

	public LeaderboardPositionDisplay display1;

	public LeaderboardPositionDisplay display2;

	protected GameParams p;

	private List<Cutscene> cutscenes = new List<Cutscene>();

	private List<Cutscene> cutscenesToPlay = new List<Cutscene>();

	private int currentPlayingCutscene;

	protected bool animFinished;

	private GGServerRequestsBackend.TextureRequest myTextureRequest;

	private GGServerRequestsBackend.TextureRequest opponentTextureRequest;

	private void Awake()
	{
		cutscenes.Clear();
		cutscenes.Add(pronounceWinner);
		cutscenes.Add(addSkill);
		cutscenes.Add(newSkillLevel);
		cutscenes.Add(exitSkillLevel);
		cutscenes.Add(addCoins);
		cutscenes.Add(missionAnim);
		cutscenes.Add(missionPassedAnim);
		cutscenes.Add(missionExitAnim);
		cutscenes.Add(gameComplete);
		foreach (Cutscene cutscene in cutscenes)
		{
			cutscene.CutsceneFinished -= CutsceneHandler;
			cutscene.CutsceneFinished += CutsceneHandler;
		}
	}

	private void PlayNextAnim()
	{
		if (currentPlayingCutscene >= cutscenesToPlay.Count)
		{
			if (p.isNextLevelReached)
			{
				UpdateStarLevel();
			}
			animFinished = true;
			touchToContinue.SetActive(true);
		}
		else
		{
			Cutscene cutscene = cutscenesToPlay[currentPlayingCutscene];
			cutscene.Play();
			currentPlayingCutscene++;
		}
	}

	public void DisableButtons()
	{
		rematchButton.gameObject.SetActive(false);
		messageRematchButton.SetActive(false);
	}

	public void DisableRematchOnly()
	{
		rematchButton.gameObject.SetActive(false);
	}

	public void RematchSent()
	{
		rematchButton.gameObject.GetComponent<UIButton>().enabled = false;
		UITools.ChangeSprite(rematchButton, "button-plastic");
		UITools.ChangeText(rematchButtonLabel, "Sent");
	}

	public void UpdateButtons(RematchStatus status)
	{
		switch (status)
		{
		case RematchStatus.Unknown:
			UITools.ChangeSprite(rematchButton, "button-plastic-green");
			UITools.ChangeText(rematchButtonLabel, "Rematch");
			rematchButton.gameObject.GetComponent<UIButton>().enabled = true;
			break;
		case RematchStatus.Accepted:
			UITools.ChangeSprite(rematchButton, "button-plastic");
			UITools.ChangeText(rematchButtonLabel, "Accept Rematch");
			rematchButton.gameObject.GetComponent<UIButton>().enabled = true;
			break;
		default:
			UITools.ChangeSprite(rematchButton, "button-plastic-red");
			UITools.ChangeText(rematchButtonLabel, "Rematch Declined");
			rematchButton.gameObject.GetComponent<UIButton>().enabled = true;
			break;
		}
	}

	private void CutsceneHandler(object sender, CutsceneEventArgs e)
	{
		PlayNextAnim();
	}

	public void ShowAnim(GameParams p)
	{
		this.p = p;
		profileImage1.gameObject.SetActive(false);
		profileImage2.gameObject.SetActive(false);
		flag1.gameObject.SetActive(true);
		flag2.gameObject.SetActive(true);
		GameConstants.SetFlag(flag1, p.flag1);
		GameConstants.SetFlag(flag2, p.flag2);
		if (GGPlayerSettings.instance.useProfilePictureInMultiplayer && BehaviourSingletonInit<FriendProfilesManager>.instance.myProfile != null)
		{
			myTextureRequest = new GGServerRequestsBackend.TextureRequest(BehaviourSingletonInit<FriendProfilesManager>.instance.myProfile.pictureUrl);
			myTextureRequest.cache = CacheStategy.CacheToFile;
			myTextureRequest.onComplete = OnMyTextureRequestComplete;
			BehaviourSingleton<GGRequestLimitter>.instance.Add(myTextureRequest);
		}
		if (GGPlayerSettings.instance.useProfilePictureInMultiplayer && p.opponentImageUrl != string.Empty)
		{
			opponentTextureRequest = new GGServerRequestsBackend.TextureRequest(BehaviourSingletonInit<FriendProfilesManager>.instance.myProfile.pictureUrl);
			opponentTextureRequest.cache = CacheStategy.GetFromCache;
			opponentTextureRequest.onComplete = OnOpponentRequestComplete;
			BehaviourSingleton<GGRequestLimitter>.instance.Add(opponentTextureRequest);
		}
		if (p.hideStars)
		{
			star1.SetActive(false);
			star2.SetActive(false);
		}
		else
		{
			star1.SetActive(true);
			star2.SetActive(true);
		}
		UITools.ChangeText(name1, p.name1);
		UITools.ChangeText(name2, p.name2);
		UITools.ChangeText(winLabel, GGFormat.FormatWinnings(p.winnings));
		string @string = ScriptableObjectSingleton<SettingsUI>.instance.GetString("Rating");
		UITools.ChangeText(player1Level, @string + ": " + p.elo1);
		UITools.ChangeText(player2Level, @string + ": " + p.elo2);
		UITools.ChangeText(levelTitle, p.prevLevelName);
		UISprite uISprite = ((p.winnerIndex != 0) ? flag2 : flag1);
		toWinnerMoney.toTrans = uISprite.cachedTransform;
		toWinnerLabel.match = uISprite.cachedTransform;
		base.gameObject.SetActive(true);
		animFinished = false;
		touchToContinue.SetActive(false);
		levelSlider.fillAmount = p.prevLevel - Mathf.Floor(p.prevLevel);
		tweenFill.from = levelSlider.fillAmount;
		tweenFill.to = p.currentLevel - Mathf.Floor(p.prevLevel);
		nextLevelTextLevel.text = "Level " + Mathf.FloorToInt(p.currentLevel);
		levelSubtitle.text = "Level " + Mathf.FloorToInt(p.prevLevel);
		missionFill.fillAmount = p.prevMissionProgress;
		missionTitle.text = p.missionName;
		tweenMissionFill.from = p.prevMissionProgress;
		tweenMissionFill.to = p.curMissionProgress;
		cutscenesToPlay.Clear();
		currentPlayingCutscene = 0;
		if (p.winnerIndex < 0)
		{
			cutscenesToPlay.Add(gameComplete);
		}
		else
		{
			cutscenesToPlay.Add(pronounceWinner);
		}
		if (p.giveMoney)
		{
			cutscenesToPlay.Add(addCoins);
		}
		if (p.isMissionProgressed)
		{
			cutscenesToPlay.Add(missionAnim);
			if (p.isMissionPassed)
			{
				cutscenesToPlay.Add(missionPassedAnim);
			}
			cutscenesToPlay.Add(missionExitAnim);
		}
		if (p.isSkillIncrease)
		{
			cutscenesToPlay.Add(addSkill);
			if (p.isNextLevelReached)
			{
				setLevelText.text = p.newLevelName;
				cutscenesToPlay.Add(newSkillLevel);
			}
			cutscenesToPlay.Add(exitSkillLevel);
		}
		PlayNextAnim();
	}

	public void OnMyTextureRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		GGServerRequestsBackend.TextureRequest textureRequest = request as GGServerRequestsBackend.TextureRequest;
		if (textureRequest != null && textureRequest == myTextureRequest)
		{
			if (request.status != 0)
			{
				myTextureRequest = null;
				return;
			}
			profileImage1.gameObject.SetActive(true);
			flag1.gameObject.SetActive(false);
			profileImage1.mainTexture = textureRequest.response;
		}
	}

	public void OnOpponentRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		GGServerRequestsBackend.TextureRequest textureRequest = request as GGServerRequestsBackend.TextureRequest;
		if (textureRequest != null && textureRequest == opponentTextureRequest)
		{
			if (request.status != 0)
			{
				opponentTextureRequest = null;
				return;
			}
			profileImage2.gameObject.SetActive(true);
			flag2.gameObject.SetActive(false);
			profileImage2.mainTexture = textureRequest.response;
		}
	}

	public void ShowDrawAnim(GameParams p)
	{
		this.p = p;
		GameConstants.SetFlag(flag1, p.flag1);
		GameConstants.SetFlag(flag2, p.flag2);
		if (p.hideStars)
		{
			star1.SetActive(false);
			star2.SetActive(false);
		}
		else
		{
			star1.SetActive(true);
			star2.SetActive(true);
		}
		UITools.ChangeText(name1, p.name1);
		UITools.ChangeText(name2, p.name2);
		UITools.ChangeText(winLabel, GGFormat.FormatWinnings(p.winnings));
		UITools.ChangeText(player1Level, p.level1);
		UITools.ChangeText(player2Level, p.level2);
		UITools.ChangeText(levelTitle, p.prevLevelName);
		UISprite uISprite = ((p.winnerIndex != 0) ? flag2 : flag1);
		toWinnerMoney.toTrans = uISprite.cachedTransform;
		toWinnerLabel.match = uISprite.cachedTransform;
		base.gameObject.SetActive(true);
		animFinished = false;
		touchToContinue.SetActive(false);
		levelSlider.fillAmount = p.prevLevel - Mathf.Floor(p.prevLevel);
		tweenFill.from = levelSlider.fillAmount;
		tweenFill.to = p.currentLevel - Mathf.Floor(p.prevLevel);
		nextLevelTextLevel.text = "Level " + Mathf.FloorToInt(p.currentLevel);
		levelSubtitle.text = "Level " + Mathf.FloorToInt(p.prevLevel);
		missionFill.fillAmount = p.prevMissionProgress;
		missionTitle.text = p.missionName;
		tweenMissionFill.from = p.prevMissionProgress;
		tweenMissionFill.to = p.curMissionProgress;
		cutscenesToPlay.Clear();
		currentPlayingCutscene = 0;
		pots.SetActive(false);
		UITools.ChangeText(drawLabel, "Draw");
		PlayNextAnim();
	}

	private void UpdateStarLevel()
	{
		UITools.ChangeText(player1Level, GGPlayerSettings.instance.Level().ToString());
		UITools.ChangeText(playerStarMatch, GGPlayerSettings.instance.Level().ToString());
	}

	public void OnContinue()
	{
		if (animFinished)
		{
			base.gameObject.SetActive(false);
			if (p.onDone != null)
			{
				p.onDone();
			}
		}
	}
}
